﻿

----------------------------
--      Localization      --
----------------------------

local L = LibStub("AceLocale-3.0"):GetLocale("BunchOfBars")

----------------------------------
--      Local Declaration      --
----------------------------------



----------------------------------
--      Module Declaration      --
----------------------------------

BunchOfBars = LibStub("AceAddon-3.0"):NewAddon("BunchOfBars", "AceEvent-3.0", "AceConsole-3.0")

local BunchOfBars = BunchOfBars

BunchOfBars.revision = tonumber(("$Revision: 40 $"):match("%d+"))
BunchOfBars.frames = {}
BunchOfBars.master = {}
-- LDB
ldb = LibStub:GetLibrary("LibDataBroker-1.1"):NewDataObject("BunchOfBars", {
	type = "launcher",
	text = "BunchOfBars",
	icon = "Interface\\Icons\\Ability_Druid_TreeofLife",
})

function ldb.OnClick(self, button)
	if button == "RightButton" then
		InterfaceOptionsFrame_OpenToCategory("BunchOfBars")
	else
		BunchOfBars.db.profile.visual.locked = not BunchOfBars.db.profile.visual.locked
	end
end
function ldb.OnTooltipShow(tt)
	tt:ClearLines()
	tt:AddLine("BunchOfBars ")
	tt:AddLine(" ")
	tt:AddDoubleLine(LOCKED..":", (BunchOfBars.db.profile.visual.locked and YES or NO), 1, 1, 0, 1, 1, 1) -- LOCKED, YES and NO are defined and translated somewher in blizz code
	tt:AddLine(" ")
	tt:AddLine(L["|cffffff00Left Click|r to lock/unlock unit frames.\n|cffffff00Right Click|r to open config frames."], 0.2, 1, 0.2)
end

----------------------------------
--      Module Functions        --
----------------------------------

function BunchOfBars:OnInitialize()
	self.db = LibStub("AceDB-3.0"):New("BunchOfBars2DB", self.defaults, "profile")

	LibStub("AceConfigRegistry-3.0"):RegisterOptionsTable("BunchOfBars", self.options)
	LibStub("AceConfigDialog-3.0"):AddToBlizOptions("BunchOfBars", "BunchOfBars")
	self.options.args.profiles = LibStub("AceDBOptions-3.0"):GetOptionsTable(self.db)
	self.options.args.profiles.disabled = function() return InCombatLockdown() end
	self.db.RegisterCallback(self, "OnProfileChanged", "OnProfileConfig")
	self.db.RegisterCallback(self, "OnProfileCopied", "OnProfileConfig")
	self.db.RegisterCallback(self, "OnProfileReset", "OnProfileConfig")

	self:RegisterChatCommand("bob", self.cmd)
	self:RegisterChatCommand("bunchofbars", self.cmd)
	mmicon = LibStub("LibDBIcon-1.0", true)


	if mmicon then
		mmicon:Register("BunchOfBars", ldb, self.db.profile.minimap)
	else
		self.options.args.minimap = nil
	end
end

function BunchOfBars:cmd(v)
	InterfaceOptionsFrame_OpenToCategory("BunchOfBars")
end

function BunchOfBars:OnEnable()
	self:HideShowParty()

	ClickCastFrames = ClickCastFrames or {}

	self:RegisterModulesOptions()

	self:ModulesOrder()
--	self:UpdateLayoutOrder()
--	self:UpdateSideOrder()

	self.frames.core = {}

	self:NewMaster()

	self:RegisterEvent("RAID_ROSTER_UPDATE", "UpdateLayouts")
	self:RegisterEvent("PLAYER_REGEN_DISABLED", "ForceUpdate")
	self:RegisterEvent("PLAYER_REGEN_ENABLED", "ForceUpdate")
	self:RegisterEvent("ZONE_CHANGED_NEW_AREA", "ForceUpdate")
	self:RegisterEvent("PARTY_MEMBERS_CHANGED", "UpdateLayouts")

	for _,module in BunchOfBars:IterateModules() do
		if module.revision > self.revision then
			self.revision = module.revision
		end
	end
	self.version = "r"..self.revision
end

function BunchOfBars:NewMaster()
 	self.master.core = self:CreateMaster("core", UIParent, self.db.profile.style)
	if self.master.core then
		self:PositionAndScale()
		self.master.core:Show()
	end
end

